#include "ljb.h"

/**
 * 建立图的邻接表存储结构
 * @param {LinkedGraph}     *g              图指针
 * @param {char}            *filename       顶点信息
 * @param {int}             *c              权值
 */
void creat(LinkedGraph *g, char *filename, int *c)
{
    int i, j, k;
    EdgeNode *s;
    FILE *fp;
    fp = fopen(filename, "r");

    if (fp)
    {
        fscanf(fp, "%d%d", &g->n, &g->e);

        // 读取顶点信息
        for (i = 0; i < g->n; i++)
        {
            fscanf(fp, "%ls", &g->adjlist[i].vertex);
            g->adjlist[i].Firstedge = NULL;
        }

        // 读取边信息
        for (k = 0; k < g->e; k++)
        {
            fscanf(fp, "%d%d", &i, &j);
            s = (EdgeNode *)malloc(sizeof(EdgeNode));
            s->adjvex = j;
            s->next = g->adjlist[i].Firstedge;
            g->adjlist[i].Firstedge = s;
            if (c == 0)
            {
                s = (EdgeNode *)malloc(sizeof(EdgeNode));
                s->adjvex = i;
                s->next = g->adjlist[j].Firstedge;
                g->adjlist[j].Firstedge = s;
            }
        }

        fclose(fp);
    }
    else
    {
        g->n = 0;
        printf("文件打开失败！");
    }
}

int main() {
    LinkedGraph g;
    int i, j = 0;
    creat(&g, "ljb.txt", &j);
    printf("图的顶点数为：%d\n", g.n);
    printf("图的边数为：%d\n", g.e);
    printf("图的邻接表为：\n");
    for (i = 0; i < g.n; i++) {
        printf("%d: %ls", i, g.adjlist[i]);
        EdgeNode *p = g.adjlist[i].Firstedge;
        while (p) {
            printf("->%d", p->adjvex);
            p = p->next;
        }
        printf("\n");
    }
    return 0;
}